#ifndef LUT2IMG
#define LUT2IMG

#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <boost/gil/image.hpp>

#include "liblut/core/LutModel.h"
#include "liblut/algo/convert2img.hpp"
#include "../../src/io/LutIgnXmlFile.h"
#include "liblut/io/ImgWriter.h"

using namespace std;
using namespace boost;
namespace po=boost::program_options;
namespace fs=boost::filesystem;

po::options_description init_desc()
{
    // Declare the supported options.
    po::options_description desc("Allowed options");
    desc.add_options()
        ("help", "produce help message")
        ("lut_file,lut", po::value<std::string>(), " input lut file (xml)")
        ("width,w", po::value<int>(), "output image width ")
        ("height,h", po::value<int>(), "output image height ")
        ("img_file,img", po::value<std::string>(), "output image filename ")
    ;
    return desc;

}

//*************************************************************
// create an RVB image file based on an xml lut file
//************************************************************
int
main(int ac, char* av[])
{
    //*******************************************************
    //declare programme variable
    fs::path m_lutPath; //path to input lut file on xml format
    fs::path m_imgPath; //path to output image file on jpg/tiff/png format
    int m_imgWitdh; //output image width
    int m_imgHeight; //output image height

    //*******************************************************
    po::options_description desc=init_desc();
    po::variables_map vm;
    po::store(po::parse_command_line(ac,av,desc),vm);

    if (vm.count("help")) {
        cout << desc << "\n";
        return 1;
    }
    //-- lut file
    if (vm.count("lut_file")) {
        cout << "input lut_file was set to "
           << vm["lut_file"].as<string>() << ".\n";
        m_lutPath=fs::path(vm["lut_file"].as<string>());
    } else {
        cout << "input lut_file was not set and is mandatory\n";
        return 1;
    }
    //-- output size
    if (vm.count("width")) {
        cout <<"output image width was set to "
           << vm["width"].as<int>() << ".\n";
        m_imgWitdh=vm["width"].as<int>();
    } else {
        cout << "output image witdth was not set and is mandatory\n";
        return 1;
    }
    if (vm.count("height")) {
        cout << "output image height was set to "
           << vm["height"].as<int>() << ".\n";
        m_imgHeight=vm["height"].as<int>();
    } else {
        cout << "output image height was not set and is mandatory\n";
        return 1;
    }
    //-- image file / output image filename
    if (vm.count("img_file")) {
        cout << "output img_file was set to "
           << vm["img_file"].as<string>() << ".\n";
        m_imgPath=fs::path(vm["img_file"].as<string>());
    } else {
        cout <<"output img_file was not set and is mandatory\n";
        return 1;
    }
    //*********************************************************
    //--------------------------------------------------------
    LutIgnXmlFile lutfile;
    boost::shared_ptr<LutModel> plut;
    std::string abs_path=fs::system_complete(m_lutPath).string();
    lutfile.open(abs_path);
    plut=lutfile.loadLut();
    //--------------------------------------------------------
    gil::rgb8_image_t img(m_imgWitdh,m_imgHeight);
    gil::rgb8_image_t::view_t lut_view= gil::view(img);
    std::cout<<" convert "<<std::endl;
    convert2img(plut, lut_view);
    std::cout<<" save "<<m_imgPath<<std::endl;
    saveImg(lut_view,m_imgPath);

    return 0;
}

#endif
